/*
Copyright 2021 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
	apimachineryconversion "k8s.io/apimachinery/pkg/conversion"
	"k8s.io/utils/ptr"
	"sigs.k8s.io/controller-runtime/pkg/conversion"

	clusterv1beta1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
	clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
	ipamv1 "sigs.k8s.io/cluster-api/api/ipam/v1beta2"
	utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

func (src *IPAddress) ConvertTo(dstRaw conversion.Hub) error {
	dst := dstRaw.(*ipamv1.IPAddress)

	return Convert_v1alpha1_IPAddress_To_v1beta2_IPAddress(src, dst, nil)
}

func (dst *IPAddress) ConvertFrom(srcRaw conversion.Hub) error {
	src := srcRaw.(*ipamv1.IPAddress)

	return Convert_v1beta2_IPAddress_To_v1alpha1_IPAddress(src, dst, nil)
}

func (src *IPAddressClaim) ConvertTo(dstRaw conversion.Hub) error {
	dst := dstRaw.(*ipamv1.IPAddressClaim)

	if err := Convert_v1alpha1_IPAddressClaim_To_v1beta2_IPAddressClaim(src, dst, nil); err != nil {
		return err
	}

	// Reset conditions from autogenerated conversions
	// NOTE: v1beta1 conditions should not be automatically be converted into v1beta2 conditions.
	dst.Status.Conditions = nil

	// Move legacy conditions (v1beta1) to the deprecated field.
	if src.Status.Conditions != nil {
		dst.Status.Deprecated = &ipamv1.IPAddressClaimDeprecatedStatus{}
		dst.Status.Deprecated.V1Beta1 = &ipamv1.IPAddressClaimV1Beta1DeprecatedStatus{}
		clusterv1beta1.Convert_v1beta1_Conditions_To_v1beta2_Deprecated_V1Beta1_Conditions(&src.Status.Conditions, &dst.Status.Deprecated.V1Beta1.Conditions)
	}

	if src.Labels != nil {
		dst.Spec.ClusterName = src.Labels[clusterv1.ClusterNameLabel]
		if dst.Annotations != nil {
			if clusterNameLabelWasSet, ok := dst.Annotations["conversion.cluster.x-k8s.io/cluster-name-label-set"]; ok {
				if clusterNameLabelWasSet == "false" {
					delete(dst.Labels, clusterv1.ClusterNameLabel)
				}
				delete(dst.Annotations, "conversion.cluster.x-k8s.io/cluster-name-label-set")
			}
		}
	}

	// Manually restore data.
	restored := &ipamv1.IPAddressClaim{}
	if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok {
		return err
	}
	dst.Status.Conditions = restored.Status.Conditions

	return nil
}

func (dst *IPAddressClaim) ConvertFrom(srcRaw conversion.Hub) error {
	src := srcRaw.(*ipamv1.IPAddressClaim)

	if err := Convert_v1beta2_IPAddressClaim_To_v1alpha1_IPAddressClaim(src, dst, nil); err != nil {
		return err
	}

	// Reset conditions from autogenerated conversions
	// NOTE: v1beta2 conditions should not be automatically be converted into legacy conditions (v1beta1).
	dst.Status.Conditions = nil

	// Retrieve legacy conditions (v1beta1) from the deprecated field.
	if src.Status.Deprecated != nil {
		if src.Status.Deprecated.V1Beta1 != nil {
			if src.Status.Deprecated.V1Beta1.Conditions != nil {
				clusterv1beta1.Convert_v1beta2_Deprecated_V1Beta1_Conditions_To_v1beta1_Conditions(&src.Status.Deprecated.V1Beta1.Conditions, &dst.Status.Conditions)
			}
		}
	}

	if src.Spec.ClusterName != "" {
		if dst.Labels == nil {
			dst.Labels = map[string]string{}
		}
		if _, ok := dst.Labels[clusterv1.ClusterNameLabel]; !ok {
			if dst.Annotations == nil {
				dst.Annotations = map[string]string{}
			}
			dst.Annotations["conversion.cluster.x-k8s.io/cluster-name-label-set"] = "false"
		}
		dst.Labels[clusterv1.ClusterNameLabel] = src.Spec.ClusterName
	}

	// Preserve Hub data on down-conversion except for metadata
	if err := utilconversion.MarshalData(src, dst); err != nil {
		return err
	}
	return nil
}

func Convert_v1alpha1_IPAddressSpec_To_v1beta2_IPAddressSpec(in *IPAddressSpec, out *ipamv1.IPAddressSpec, s apimachineryconversion.Scope) error {
	if err := autoConvert_v1alpha1_IPAddressSpec_To_v1beta2_IPAddressSpec(in, out, s); err != nil {
		return err
	}
	out.Prefix = ptr.To(int32(in.Prefix))
	return nil
}

func Convert_v1beta2_IPAddressSpec_To_v1alpha1_IPAddressSpec(in *ipamv1.IPAddressSpec, out *IPAddressSpec, s apimachineryconversion.Scope) error {
	if err := autoConvert_v1beta2_IPAddressSpec_To_v1alpha1_IPAddressSpec(in, out, s); err != nil {
		return err
	}
	out.Prefix = int(ptr.Deref(in.Prefix, 0))
	return nil
}

func Convert_v1beta2_IPAddressClaimSpec_To_v1alpha1_IPAddressClaimSpec(from *ipamv1.IPAddressClaimSpec, to *IPAddressClaimSpec, scope apimachineryconversion.Scope) error {
	return autoConvert_v1beta2_IPAddressClaimSpec_To_v1alpha1_IPAddressClaimSpec(from, to, scope)
}

func Convert_v1beta2_IPAddressClaimStatus_To_v1alpha1_IPAddressClaimStatus(from *ipamv1.IPAddressClaimStatus, to *IPAddressClaimStatus, scope apimachineryconversion.Scope) error {
	return autoConvert_v1beta2_IPAddressClaimStatus_To_v1alpha1_IPAddressClaimStatus(from, to, scope)
}
